package com.pangda.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.domain.PageResult;
import com.pangda.common.utils.UserContext;
import com.pangda.mapper.ClueMapper;
import com.pangda.mapper.ClueReportMapper;
import com.pangda.pojo.Business;
import com.pangda.pojo.Clue;
import com.pangda.pojo.ClueReport;
import com.pangda.pojo.Contract;
import com.pangda.pojo.dto.ClueDto;
import com.pangda.pojo.dto.ClueReportDto;
import com.pangda.pojo.vo.ClueVo;
import com.pangda.pojo.vo.CluesStatistics;
import com.pangda.pojo.vo.ContractVo;
import com.pangda.pojo.vo.salesVo;
import com.pangda.service.IBusinessService;
import com.pangda.service.IClueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.List;

/**
 * <p>
 * 线索表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-21
 */
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements IClueService {


    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueReportMapper clueReportMapper;
    @Autowired
    private IBusinessService businessService;

    @Override
    public void addRecored(ClueReportDto dto) {
       //根据获取的线索id更新线索信息
        Clue clue = clueMapper.selectById(dto.getClueId());
        Clue clue1 = new Clue();
        BeanUtil.copyProperties(dto,clue1);
        BeanUtil.copyProperties(clue,clue1);
        clue1.setSubject(dto.getSubject());
        clue1.setNextTime(dto.getNextTime());
        clue1.setStatus("2");
        clue1.setOwner(null);
        clueMapper.updateById(clue1);
        //插入线索跟进记录
        ClueReport clueReport = BeanUtil.copyProperties(dto, ClueReport.class);
        clueReport.setCreateBy(UserContext.getUser());
        clueReport.setCreateTime(LocalDateTime.now());
        clueReportMapper.insert(clueReport);

    }

    @Override
    public PageResult cluesStatisticsList(PageResult pageResult, ClueDto dto) {
        Page<Clue> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        LambdaQueryWrapper<Clue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getOwner()), Clue::getOwner, dto.getOwner())
                .eq(ObjectUtil.isNotEmpty(dto.getId()), Clue::getId, dto.getId())
                .between(ObjectUtil.isNotEmpty(dto.getEndCreateTime()) && ObjectUtil.isNotEmpty(dto.getBeginCreateTime())
                        ,Clue::getCreateTime , dto.getBeginCreateTime(), dto.getEndCreateTime()+" 23:59:59");
        Page<Clue> cluePage = clueMapper.selectPage(page, wrapper);
        PageResult<CluesStatistics> success = PageResult.success(cluePage, CluesStatistics.class);
        success.getRows().stream().forEach(clue ->{
            if (clue.getClueStatus().equals("6")){
                Business one = businessService.getOne(new LambdaQueryWrapper<Business>().eq(Business::getClueId, clue.getId()));
                if (ObjectUtil.isNotEmpty(one)){
                    clue.setBusinessStatus(one.getStatus());
                }
            }
        });

        return success ;

    }

    @Override
    public List<salesVo> getTime(String beginCreateTime, String endCreateTime) {
        endCreateTime = endCreateTime + " 23:59:59";
        List<salesVo> salesVos = clueMapper.getTime(beginCreateTime, endCreateTime);

        return salesVos;
    }

    @Override
    public Map<String, Object> cluesStatistics(String beginCreateTime, String endCreateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; // ISO格式，例如 "2023-01-01"
        // 使用DateTimeFormatter来解析字符串为LocalDate
        LocalDate beginDate =  LocalDate.parse(beginCreateTime, formatter);
        LocalDate endDate =  LocalDate.parse(endCreateTime, formatter);
        List<Map<String,Object>> list = clueMapper.cluesStatistics(beginDate,endDate);
        Map<String, Object> response = new HashMap<>();
        ArrayList<String> dates = new ArrayList<>();
        ArrayList<Long> nums = new ArrayList<>();
        ArrayList<Map<String,Object>> m = new ArrayList<>();
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("name","新增线索数量");
        stringObjectMap.put("data",nums);
        Map<String, Object> m2 = new HashMap<>();
        m2.put("name","线索总数");
        m.add(stringObjectMap);
        m.add(m2);
        list.stream().forEach(map ->{
            String i = String.valueOf(map.get("time"));
            Long o = (Long) map.get("num");
            dates.add(i);
            nums.add(o);
        });
        List<Map<String,Object>> list1 = clueMapper.ClueTotal(beginDate,endDate);
        ArrayList<BigDecimal> nums1 = new ArrayList<>(nums.size());
        list1.stream().forEach(map ->{
            BigDecimal o = (BigDecimal) map.get("total");
            nums1.add(o);
        });
        m2.put("data",nums1);
        response.put("xAxis",dates);
        response.put("series",m);
        return response;
    }
}
